/*
 Copyright 2014 Alexander V. Nikulin
 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
	
 	http://www.apache.org/licenses/LICENSE-2.0
	
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
*/
package ru.avn.adf;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.LinkedList;
import java.util.List;

/**
 * @author Alexander V. Nikulin
 */
public class AdfExecutorFactory {
	
	private AdfExecutorConfiguration config;
	
	private AdfExecutorFactory () {}
	
	public static AdfExecutorFactory getFactory(AdfExecutorConfiguration config) {
		
		if (config == null) {
			throw new IllegalArgumentException("AdfMapperConfiguration is null");
		}
		
		AdfExecutorFactory factory = new AdfExecutorFactory();
		factory.config = config;
		return factory;
	}
	
	
	public AdfExecutor createExecutor(Class<?> type) {
			
		if (type == null) {
			throw new IllegalArgumentException("type is null");
		}
		
		List<PayloadDeclaration<Annotation>> declarations = new LinkedList<PayloadDeclaration<Annotation>>();
			
		Class<?> currentType = type;
		
		do {
	
			for (Annotation annotation: currentType.getAnnotations()) {
				if (config.isAnnotationBinded(annotation.annotationType())) {
					declarations.add(PayloadDeclarations.newPayloadDeclaration(annotation, currentType));
				}
			}
				
			
			for (Field field : currentType.getDeclaredFields()) {
				for (Annotation annotation: field.getAnnotations()) {
					if (config.isAnnotationBinded(annotation.annotationType())) {
						declarations.add(PayloadDeclarations.newPayloadDeclaration(annotation, field));
					}
				}
			}
			
			currentType = currentType.getSuperclass();
			
		} while (currentType != null);

		
		for (Method method : type.getMethods()) {
			for (Annotation annotation: method.getAnnotations()) {
				if (config.isAnnotationBinded(annotation.annotationType())) {
					declarations.add(PayloadDeclarations.newPayloadDeclaration(annotation, method));
				}
			}
		}
		
		PayloadBodyExtractorFactory extractorFactory = PayloadBodyExtractorFactory.getFactory(type);
		
		List<PayloadProcessorInvoker> invokers = new LinkedList<PayloadProcessorInvoker>();
		
		for (PayloadDeclaration<Annotation> declaration : declarations) {
			
			PayloadProcessor<Annotation> processor = decorateProcessor(newProcessor(declaration));
			processor.initialize(declaration);
			 					 	
			PayloadProcessorInvoker invoker = PayloadProcessorInvoker.newBuilder()
					.extractor(extractorFactory.getPayloadBodyExtractor(declaration))
					.processor(processor)
					.build();
		 				
			invokers.add(invoker);
			
		}
		
		return decorateExecutor(newExecutor(invokers));
	}
	
	private AdfExecutor newExecutor(List<PayloadProcessorInvoker> invokers) {
		return new DefaultAdfExecutor(invokers);
	}
	
	private AdfExecutor decorateExecutor(AdfExecutor executor) {
		Class<AdfExecutorDecorator> decoratorType = config.getExecutorDecorator();
		if (decoratorType != null) {
			AdfExecutorDecorator decorator = ReflectionUtils.newInstance(decoratorType, new Object[] {executor}, new Class[] {AdfExecutor.class});
			executor = decorator;
		}
		
		return executor;
	}
	
	private PayloadProcessor<Annotation> newProcessor(PayloadDeclaration<Annotation> declaration) {
		Class<PayloadProcessor<Annotation>> processorType = config.getPayloadProcessor(declaration.getAnnotation().annotationType());
	 	PayloadProcessor<Annotation> processor = ReflectionUtils.newInstance(processorType);
		return processor;
	}
	
	private PayloadProcessor<Annotation> decorateProcessor(PayloadProcessor<Annotation> processor) {
		Class<PayloadProcessorDecorator> decoratorType = config.getProcessorDecorator();
	 	
	 	if (decoratorType != null) {
	 		PayloadProcessorDecorator decorator = ReflectionUtils.newInstance(decoratorType , new Object[] {processor}, new Class[] {PayloadProcessor.class});
	 		processor = decorator;
	 	}
			
	 	return processor;
	}
		
}
